package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.netEase.dao.orderDao;
import org.netEase.entity.order;
import org.netEase.util.BaseDao;

public class orderDaoImpl extends BaseDao implements orderDao {

	@Override
	public int addO(order order) {
		String sql = "insert into order_list(olid,ono,hid,money,osid,dzid,payMethod)values(?,?,?,?,?,?,?)";
		return super.executeUpdate(sql, order.getOlid(),order.getOno(),order.getHid(),order.getMoney(),order.getOsid(),order.getDzid(),order.getPaymethod());
	}

	@Override
	public int updbyoid(String orderid,String paymethod) {
		String sql = "update order_list set osid = 2,payMethod=? where olid = ?";
		return super.executeUpdate(sql,paymethod,orderid);
	}

	@Override
	public List<order> getbyhid(int pageNO,int sizes,int hid,int osid,String InputorderId) {
		int pageNOS = (pageNO - 1)*sizes;
		String sql = "select * from order_list where 1=1";
		List<Object> condition = new ArrayList<>();
		if (osid >0) {
			sql+=" and osid = ?";
			condition.add(osid);
		}
		if (InputorderId != "" && InputorderId != null) {
			sql+=" and olid = ?";
			condition.add(InputorderId);
		}
		sql+=" and hid = ?";
		sql+=" order by olDate desc limit ?,?";
		condition.add(hid);
		condition.add(pageNOS);
		condition.add(sizes);
		List<order> list = new ArrayList<order>();
		ResultSet rs = super.executeQuery(sql, condition.toArray());
		try {
			while (rs.next()) {
				order order = new order();
				order.setDzid(rs.getInt("dzid"));
				order.setHid(rs.getInt("hid"));
				order.setMoney(rs.getDouble("money"));
				order.setOlDate(rs.getTimestamp("olDate"));
				order.setOlid(rs.getString("olid"));
				order.setOno(rs.getInt("ono"));
				order.setOsid(rs.getInt("osid"));
				order.setPaymethod(rs.getString("paymethod"));
				
				list.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public int totCount(int hid, int osid,String InputorderId) {
		String sql = "select count(*) c from order_list where 1=1";
		int totcount = 0;
		List<Object> condition = new ArrayList<>();
		if (osid > 0) {
			sql+=" and osid = ?";
			condition.add(osid);
		}
		if (InputorderId != "" && InputorderId != null) {
			sql+=" and olid = ?";
			condition.add(InputorderId);
		}
		sql+=" and hid = ?";
		condition.add(hid);
		ResultSet rs = super.executeQuery(sql, condition.toArray());
		try {
			if (rs.next()) {
				totcount = rs.getInt("c");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return totcount;
	}

	@Override
	public int updbyoidtow(String orderid, int hid) {
		String sql = "update order_list set osid = 4 where olid = ? and hid = ?";
		return super.executeUpdate(sql, orderid,hid);
	}

	@Override
	public double getbyoid(String orderid) {
		double d =0;
		String sql = "select money from order_list where olid = ?";
		ResultSet rs = super.executeQuery(sql, orderid);
		try {
			if (rs.next()) {
				d = rs.getDouble("money");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return d;
	}

}
